package pers.fish.sso.server.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import pers.fish.sso.common.model.Authentication;
import pers.fish.sso.common.util.CookieUtil;
import pers.fish.sso.common.util.TicketUtil;
import pers.fish.sso.server.exception.AuthCodeException;
import pers.fish.sso.server.exception.BadCrendentialException;
import pers.fish.sso.server.mapping.TicketDataMappingStorage;
import pers.fish.sso.server.service.ILoginService;
import pers.fish.sso.server.service.impl.DefaultLoginServiceImpl;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录认证controller
 *
 * @author fish
 * @date 2020/1/29 11:50
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @Autowired
    private TicketDataMappingStorage ticketDataMappingStorage;

    @GetMapping("/toLogin")
    public String toLogin(HttpServletRequest request, Model model, String service,String error) {

        model.addAttribute("service", service);
        if(StringUtils.isNotBlank(error)){
            model.addAttribute("error", error);
            return "login";
        }

        String tgt = CookieUtil.getCookieValue(request,ILoginService.TGT_COOKIE_NAME);

        if (!StringUtils.isBlank(tgt)) {
            //检查ticket是否有效
            Authentication authentication = ticketDataMappingStorage.getAuthenticationByTgt(tgt);

            if (authentication != null && !StringUtils.isBlank(service)) {
                StringBuilder redirectUrl = new StringBuilder(service);
                String ticket = TicketUtil.generateTicket();
                ticketDataMappingStorage.storageAuthentication(ticket,tgt,authentication);
                redirectUrl.append("?ticket=").append(ticket);
                return "redirect:" + redirectUrl.toString();
            }else if(StringUtils.isBlank(service)){
                return "success";
            }

        }

        return "login";
    }

    @GetMapping("/success")
    public String success() {
        return "success";
    }

    @PostMapping("/login")
    public String login(String service,String username,Model model) {

        try {
            String result = loginService.login(username);
            return result;
        }catch (BadCrendentialException | AuthCodeException e){
            return "redirect:/login/toLogin?service="+service+"&error="+e.getMessage();
        } catch (Exception e){
            throw e;
        }

    }



}
